//
//(C) Copyright 2011 HongKong Airline.,Ltd. 
//
/**
 * orderforminfoAction
 * 项目名称:代购系统
 * @author Mengxiangliang
 * 功能描述:
 * 历史版本:2013-01-04 v1.0.0 Mengxiangliang  创建:
 * @version [V1] 
 */
package com.gamutsoft.amb.orderinfo.action;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.gamutsoft.amb.base.action.BaseAction;
import com.gamutsoft.amb.commodity.model.Commodity;
import com.gamutsoft.amb.commodity.service.ICommodityService;
import com.gamutsoft.amb.common.bean.QueryExample;
import com.gamutsoft.amb.common.constants.Constants;
import com.gamutsoft.amb.common.exception.BusinessException;
import com.gamutsoft.amb.orderinfo.bean.CommitOrderBean;
import com.gamutsoft.amb.orderinfo.bean.OrderforminfoQueryBean;
import com.gamutsoft.amb.orderinfo.model.Orderforminfo;
import com.gamutsoft.amb.orderinfo.service.IOrderforminfoService;
import com.gamutsoft.amb.user.model.SystemUser;


@SuppressWarnings({"serial","rawtypes"})
public class OrderforminfoAction extends BaseAction {
	
	private Orderforminfo orderforminfo;
	
	private List<Map> orderforminfoList;
	
	private IOrderforminfoService orderforminfoService;
	
	List<Commodity> commodityList;
	
	private ICommodityService commodityService;
	
	private OrderforminfoQueryBean orderforminfoQueryBean;
	
	private Map<String,List<Map>> statisticMap;
	
	private Map<String,Double> totalMap;
	
	private CommitOrderBean searchBean;
	private String goodsIds;
	private List<Commodity> orderCommodityList;
	/**
	 * 初始化订单
	 */
	public String initOrderInfo()throws BusinessException, Exception{
		orderCommodityList=new ArrayList();
		totalMap=new HashMap();
		double sum=0.00;
		if(goodsIds!=null && !"".equals(goodsIds)){
			  String[] ids=goodsIds.split(",");
			  for(String str:ids){
				  if(str!=null && !"".equals(str)){
					  Commodity domain=commodityService.get(Long.parseLong(str));
					  if(domain!=null){
						  orderCommodityList.add(domain);
						  sum+=domain.getBuycount()*domain.getPrice().doubleValue()+domain.getFreight().doubleValue();
					  }
				  }
			  }
			  totalMap.put("totalAmount", sum);
		}
		return "init_create_orderinfo";
	}
	/**
	 * 提交订单
	 */
	public String createOrder()throws BusinessException, Exception{
		try{
			if(commodityList!=null && commodityList.size()!=0){
				orderforminfoService.createOrderforminfo(commodityList, super.getSystemUser());
			}
		}catch(Exception e){
			log.error(e,e);
		}
		return "search_orderforminfo_success";
	}
    /**
     * 查看订单
     */
	public String viewMyOrder()throws BusinessException, Exception{
		try{
			if(orderforminfo!=null && orderforminfo.getId()!=null && !"".equals(orderforminfo.getId())){
			  commodityList=commodityService.searchByOrderId(orderforminfo.getId());
			}
		}catch(Exception e){
			log.error(e,e);
		}
		return "";
	}
	/**
	 * 查询我的订单(当前的订单)
	 */
	public String searchMyOrderInfo(){
		try{
			SystemUser user=super.getSystemUser();
			Long userId=user.getId();
			statisticMap=new HashMap();
			//查询待处理订单
			statisticMap.put("noBuy", orderforminfoService.searchOrderforminfoByStatus(new QueryExample(), Constants.COMMODITY_ORDER_NOBUY, userId));
			//查询处理中订单
			statisticMap.put("dealing", orderforminfoService.searchOrderforminfoByStatus(new QueryExample(), Constants.COMMODITY_ORDER_DEALING, userId));
			//查询审核中订单
			statisticMap.put("checking", orderforminfoService.searchOrderforminfoByStatus(new QueryExample(), Constants.COMMODITY_ORDER_CHECKING, userId));
			//查询已订购订单
			statisticMap.put("buying", orderforminfoService.searchOrderforminfoByStatus(new QueryExample(), Constants.COMMODITY_ORDER_BUYING, userId));
			//查询已到货订单
			statisticMap.put("apprival", orderforminfoService.searchOrderforminfoByStatus(new QueryExample(), Constants.COMMODITY_ORDER_ARRIVAL, userId));
			//查询已入库订单
			statisticMap.put("storage", orderforminfoService.searchOrderforminfoByStatus(new QueryExample(), Constants.COMMODITY_ORDER_STORAGE, userId));
			//查询全部订单
			statisticMap.put("ALL", orderforminfoService.searchOrderforminfoByStatus(new QueryExample(),null, userId));
		
			//查询需要处理的订单
			QueryExample qe=new QueryExample() ;
			qe.addCondictions("T.STATUS",">=",Constants.COMMODITY_OPERAGE_NOGOOD);
			statisticMap.put("operateDealing", orderforminfoService.searchOrderforminfoByStatus(qe,null, userId));
			//查询无货的订单
			statisticMap.put("noGood", orderforminfoService.searchOrderforminfoByStatus(new QueryExample(),Constants.COMMODITY_OPERAGE_NOGOOD, userId));
			//查询暂时缺货的订单
			statisticMap.put("tempNoGood", orderforminfoService.searchOrderforminfoByStatus(new QueryExample(),Constants.COMMODITY_OPERAGE_OUTOFSTOCK, userId));
			//查询无效的订单
			statisticMap.put("invalid", orderforminfoService.searchOrderforminfoByStatus(new QueryExample(),Constants.COMMODITY_OPERAGE_INVALID, userId));
			//查询退换货
			statisticMap.put("returnGood", orderforminfoService.searchOrderforminfoByStatus(new QueryExample(),Constants.COMMODITY_OPERAGE_RETURNS, userId));
			//查询已退货
			statisticMap.put("hasReturn", orderforminfoService.searchOrderforminfoByStatus(new QueryExample(),Constants.COMMODITY_OPERAGE_HASRETURNED, userId));
			//查询补款中
			statisticMap.put("replenishment", orderforminfoService.searchOrderforminfoByStatus(new QueryExample(),Constants.COMMODITY_OPERAGE_REPLENISHMENT, userId));
			//联系卖家中
			statisticMap.put("contact", orderforminfoService.searchOrderforminfoByStatus(new QueryExample(),Constants.COMMODITY_OPERAGE_CONTACTING, userId));
			//问题订单
			statisticMap.put("problem", orderforminfoService.searchOrderforminfoByStatus(new QueryExample(),Constants.COMMODITY_OPERAGE_PROBLEMORDER, userId));
			
			if(searchBean==null){
				searchBean=new CommitOrderBean();
				searchBean.setUserId(userId);
			}else{
				if(searchBean.getSearchMonth()!=null && !"".equals(searchBean.getSearchMonth())){
					if(searchBean.getSearchMonth().equals("1")){
						searchBean.setSearchMonth(new SimpleDateFormat("yyyy-MM").format(new Date()));
					}
				}
			}
			orderforminfoList=orderforminfoService.searchOrderforminfoByWhere(new QueryExample(), searchBean);
			dealTotalMap(orderforminfoList);
		}catch(Exception e){
			log.error(e,e);
		}
		return "search_orderforminfo_success";
	}
	private void dealTotalMap(List<Map>list){
		totalMap=new HashMap();
		double sum=0.00;
		double goodSum=0.00;
		double freightSum=0.00;
		if(list!=null && list.size()!=0){
			for(Map map:list){
				double count=dealMapForNum(map,"buycount");
				double price=dealMapForNum(map,"price");
				double freight=dealMapForNum(map,"freight");
				goodSum+=price*count;
				freightSum+=freight;
				sum+=price*count+freight;
			}
		}
		totalMap.put("totalAmount", sum);
		totalMap.put("goodAmount", goodSum);
		totalMap.put("freightAmount", freightSum);
	}
	private double dealMapForNum(Map map,String key){
		double result=0.00;
		if(map!=null){
			result= map.get(key)==null?0.00:Double.parseDouble(map.get(key).toString());
		}
		return result;
	}
	/**
	 * 查询历史订单
	 */
	public String searchHistoryOrderInfo(){
		try{
			SystemUser user=super.getSystemUser();
			Long userId=user.getId();
			if(searchBean==null){
				searchBean=new CommitOrderBean();
				searchBean.setUserId(userId);
			}
			searchBean.setStatus(Constants.COMMODITY_ORDER_SUCCESS);//订单完成状态
			orderforminfoList=orderforminfoService.searchOrderforminfoByWhere(new QueryExample(), searchBean);
		}catch(Exception e){
			log.error(e,e);
		}
		return "";
	}
	/**
	 * @return the orderforminfo
	 */
	public Orderforminfo getOrderforminfo() {
		return orderforminfo;
	}

	/**
	 * @param orderforminfo the orderforminfo to set
	 */
	public void setOrderforminfo(Orderforminfo orderforminfo) {
		this.orderforminfo = orderforminfo;
	}

	/**
	 * @return the orderforminfoList
	 */
	public List<Map> getOrderforminfoList() {
		return orderforminfoList;
	}

	/**
	 * @param orderforminfoList the orderforminfoList to set
	 */
	public void setOrderforminfoList(List<Map> orderforminfoList) {
		this.orderforminfoList = orderforminfoList;
	}

	/**
	 * @param orderforminfoService the orderforminfoService to set
	 */
	public void setOrderforminfoService(IOrderforminfoService orderforminfoService) {
		this.orderforminfoService = orderforminfoService;
	}

	public List<Commodity> getCommodityList() {
		return commodityList;
	}

	public void setCommodityList(List<Commodity> commodityList) {
		this.commodityList = commodityList;
	}
	public ICommodityService getCommodityService() {
		return commodityService;
	}
	public void setCommodityService(ICommodityService commodityService) {
		this.commodityService = commodityService;
	}
	public OrderforminfoQueryBean getOrderforminfoQueryBean() {
		return orderforminfoQueryBean;
	}
	public void setOrderforminfoQueryBean(
			OrderforminfoQueryBean orderforminfoQueryBean) {
		this.orderforminfoQueryBean = orderforminfoQueryBean;
	}
	
	public Map<String, List<Map>> getStatisticMap() {
		return statisticMap;
	}
	public void setStatisticMap(Map<String, List<Map>> statisticMap) {
		this.statisticMap = statisticMap;
	}
	public Map<String, Double> getTotalMap() {
		return totalMap;
	}
	public void setTotalMap(Map<String, Double> totalMap) {
		this.totalMap = totalMap;
	}
	public CommitOrderBean getSearchBean() {
		return searchBean;
	}
	public void setSearchBean(CommitOrderBean searchBean) {
		this.searchBean = searchBean;
	}
	public String getGoodsIds() {
		return goodsIds;
	}
	public void setGoodsIds(String goodsIds) {
		this.goodsIds = goodsIds;
	}
	public List<Commodity> getOrderCommodityList() {
		return orderCommodityList;
	}
	public void setOrderCommodityList(List<Commodity> orderCommodityList) {
		this.orderCommodityList = orderCommodityList;
	}
	
}
